﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExpressionEvaluator;

namespace ExprEvaluatorTestForm
{
    public partial class Form1 : Form
    {
        private ExprEvaluator _exprEval = null;

        private class typee{
            public decimal apple = 5;
    }

        public Form1()
        {
            InitializeComponent();


            _exprEval = new ExprEvaluator();
            //create a V1 variable that adds the input value + 3
            _exprEval.Variables.Add("V1", (obj) =>
            {
                var inputDec = obj as decimal?;
                if (inputDec.HasValue)
                    return inputDec.Value + 3;
                else
                    return 1;
            });

            //create a V2 variable that multiplies the input value * 3
            _exprEval.Variables.Add("V2", (obj) =>
            {
                var inputDec = obj as decimal?;
                if (inputDec.HasValue)
                    return inputDec.Value * 2;
                else
                    return 1;
            });

            _exprEval.Constants.Add("C1", 5.05M);//Constant C1 as 5.05
            _exprEval.Constants.Add("C4", 6);//Constant C4 as 6
            //dont name these the same as your variables- constants will trump variables

            //initial txtbox values
            txtInputVal.Text = "1";
            txtPrecVal.Text = "1";
            txtConvVal.Text = "1";


            _exprEval.AddInlineFunction("V2", 1, (decimal[] args) =>
            {
                return args[0] + 3;
            });
            _exprEval.AddInlineFunction("FN1", "test function", 2, (decimal[] args) =>
            {
                return args[1] + 3;
            });
        }

        private void btnInfo_Click(object sender, EventArgs e)
        {
            StringBuilder strBIninefunctions = new StringBuilder();
            foreach (KeyValuePair<string, string> kv in _exprEval.InLine_FunctionDescriptions)
            {
                strBIninefunctions.AppendLine(string.Format("       {0} -> {1}",kv.Key, kv.Value));
            }

            MessageBox.Show(
                string.Format(@"Details on test expression form
Variables: 
    V1: (Input Val) + 3    
    V2: (Input Val) * 2
Constants 
    C1: 5.05
    C4: 6
InLine functions:
{0}
                ",strBIninefunctions.ToString())
             );
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                SetUpExpressionObj(true);
                txtOutput.Text = _exprEval.PerformCalculation(true).ToString();
            }
            catch (ExprEvaluatorException epEx)
            {
                MessageBox.Show(epEx.Message);
            }               
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            try
            {
                SetUpExpressionObj(true);
                MessageBox.Show(_exprEval.Validate().ToString());
            }
            catch (ExprEvaluatorException epEx)
            {
                MessageBox.Show(epEx.Message);
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                SetUpExpressionObj();
                txtOutput.Text = _exprEval.PerformCalculation().ToString();
            }
            catch (ExprEvaluatorException epEx)
            {
                MessageBox.Show(epEx.Message);
            }
        }

       
        private void SetUpExpressionObj(bool isTest = false)
        {
            decimal dInputVal, dConversionFactor;
            int iPrecision;

            if (!decimal.TryParse(txtInputVal.Text, out dInputVal))//if input val failes use 1
            { 
                txtInputVal.Text = "1";//initial txtbox values
                dInputVal = 1;
            }
            if (!decimal.TryParse(txtConvVal.Text, out dConversionFactor))//if convert factor failed ise 1
            {

                txtConvVal.Text = "1";//initial txtbox values
                dConversionFactor = 1;
            }
            if (!int.TryParse(txtPrecVal.Text, out iPrecision))//if Precision failed use 1
            {
                txtPrecVal.Text = "1";//initial txtbox values
                iPrecision = 1;
            }

             _exprEval.Expression = txtExpression.Text;
            
            if(isTest)
                _exprEval.InputObj = null;// not needed for test... just as example
            else
                _exprEval.InputObj = dInputVal;

            _exprEval.Precision = iPrecision;

            _exprEval.ConversionFactor= dConversionFactor;

        }

      

    }
}
